//Use jQuery Ajax to fetch the file
;jQuery.extend(
       {
          getExtFile: function(url) 
          {
              var result = null;
              $.ajax(
              {
                url: url,
                type: 'get',
                dataType: 'html',
                async: false,
                cache: false,
                success: function(data) 
                {
                    result = data;
                }
              });
          return result;
          }
       }
    );






function isUserType(type){
  if (type == 'c' || type == 'C') {
    return false;
  } else if (type == 'u' || type == 'U') {
    return true;
  }
}


function ss_parseIntNaN(val)
{
    return (isNaN(val)||val=="")?0:parseInt(val);
}

function ssshowInput(ds,isShow)
{
  ds.set("showInput",isShow);
  ds.set("showInputArea",!isShow);
}


function ss_formartDate(sdate)
{
  return sdate;//20140524
  if(sdate=="") return sdate;
  return new Date(sdate).format("dd/MM/yyyy hh:mm:ss TT");
}

function ss_isUndefined(val)
{
  return false;
  if((typeof(val) == 'undefined') || val==''){
    return true;
  }
  return false;
}


var FileViewer = (function(){

  var imageTmpl = "<div id='fileviewer_window_div' class='wp_h_482 wp_w_640' style='overflow:scroll;'><img id='fileviewer_window_div_img' class='wp_h_482 wp_w_640' src='#:data.url#' alt='' /></div>";
  //var txtTmpl = "<div id='file'></div>";
  

  return {
    load:function(url,fileName,fileExt,form){
      var data = [];
      var w = 640;
      var h = 482;


      // var fileExt = url.replace(/^.*[\\\.]/, '').toUpperCase();
      // var fileName = url.replace(/^.*[\\\/]/, '');

      var donwloadFileType = "pdf, doc, docx, ppt, pptx, xls, xlsx, zip, txt, tiff , tif";

      if(donwloadFileType.search(fileExt.toLowerCase())!=-1){
        window.open(url);
        return ;
      }
      var template = kendo.template(imageTmpl);
      data = {url:url,filename:fileName};

      var kendoWindow = $("<div id='fileviewer_window' />").kendoWindow({
            width:w,
            height:h,
            visible:false,
            resizable: false,
            draggable: false,
            modal: true,
            actions:["Close"],
            close:function(){
              $("#fileviewer_window").remove();
            },
            title    : fileName,
            viewable : false,
            content  : {
                dataType: "json",
                template: template(data)
            }
        }).data("kendoWindow");
        
        //scroll
        var useragent = Workport.getBrowser();
        if(useragent != 'ipad'){
              $("#fileviewer_window_div").slimScroll({
                  height: h,
                  width: w,
                  disableFadeOut:true,
                  wheelStep: 1
              });
        }       
        //bind data
        kendo.bind(kendoWindow.data, data);
        var imgFileType = "jpg, jpeg, png, gif, bmp, tiff ,tif ";
        
        /*
        // load txt file
        if(imgFileType.toUpperCase().search(fileExt)!=-1){

        }else if(fileExt == "txt".toUpperCase()){
          $('#fileviewer_window_div_img').hide();
          $.ajax({
              url : url,
              dataType: "text",
              success : function (data) {
                  $("#fileviewer_window_div").html(data);
              }
          });
        }else {
          alert("FileViewer : this is a undefined file.");
          return ;
        }
        */
        kendoWindow.open().center();
		if(form)
			$("#fileviewer_window").prev().find("span.k-i-close").attr("id",form);
    }
  };
}());


function ss_popup_url(_title,url){
  if(url&&url!=""){
  var popup_win = $('<div id="ss_topic_app_detail_window"><div id="ss_topic_app_datail_content"><iframe id="ss_topic_iframe"  frameborder="0" width="656" height="550" src=""></iframe></div></div>');

  popup_win.kendoWindow({
    width:658,
    height:555,
    title:_title,
    visible:false,
    resizable: false,
    draggable: false,
    modal: true,
    actions:["Close"],
    close:function(){
      $("#ss_topic_app_detail_window").empty();
      //$("#iwantto_app_datail_content").append("<iframe id='iwantto_apps_tabs_iframe'  frameborder='0' width='656' height='550'></iframe>");
    }
  });
  var win = $("#ss_topic_app_detail_window").data("kendoWindow");
  win.title(_title);
    $("#ss_topic_iframe").attr("src",url);
    popup_win.data("kendoWindow").center();
    popup_win.data("kendoWindow").open();
  }else{
    Workport.utils.alertMsg(alert_title,"Url is NULL.",function(_data){});
  }
}

